package com.boarsoft.boar.batch.demo.flow.bigfile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.boarsoft.flow.core.bean.JoinHandler;

/**
 * @ClassName: QryDpfm01Join   
 * @Description:  分库分表查询完后进行汇总
 */
public class QryDpfm01Join implements JoinHandler {
	private static final Logger log = LoggerFactory.getLogger(QryDpfm01Join.class);

	@Override
	public Object join(Object flowData, Map<String, Object> resultMap) {
		// 汇总后的查询结果可以进行统计，修改等操作，这里将查询结果保存在文件
		try {
			
			OutputStream os;
//			os = new FileOutputStream("c:\\jxpoc\\dpfm01QueryResult.dat");
			os = new FileOutputStream("/home/app/data/dpfm01QueryResult.dat");

			for (Entry<String, Object> entry : resultMap.entrySet()) {
				System.out.println("joined key: " + entry.getKey());
				List<Object[]> lst = (List<Object[]>) entry.getValue();

				for (Object[] objects : lst) {
					String str = Arrays.toString(objects)+"\r\n";
					os.write(str.getBytes(), 0, str.length());
//					System.out.println(Arrays.toString(objects));
				}
			}

			os.flush();
			os.close();
			System.out.println("查询dpfm01共耗时：" + (System.currentTimeMillis() - QryDpfm01Fork.qryDpfm01ForkStart) + "ms.");

		} catch (IOException e) {
			log.info(e.getMessage());
		}

		return resultMap;
	}
}
